import { getUserDataPath } from './utils/getUserDataPath';

const axios = require('axios');
const Store = require('electron-store');

const isAxiosError = (e) => {
  return e.isAxiosError === true;
};

const store = new Store({
  name: 'sync',
  cwd: getUserDataPath(),
});

class Http {
  async request(method, url, data = {}, options = {}) {
    const { params, headers = {} } = options;
    const user = store.get('user');
    axios.defaults.headers.common.Authorization = user.accessToken;
    axios.defaults.headers.common['authorization-refresh'] = user.refreshToken;

    try {
      const response = await axios.default.request({
        url: `${store.get('baseOrigin')}/api-gateway${url}`,
        method,
        data,
        params,
        timeout: 600000,
      });
      return response.data.data;
    } catch (err) {
      if (isAxiosError(err)) {
        if (err.response) {
          console.log(err);
        }
      }
      throw err;
    }
  }
}

export default Http;
// module.exports = Http;
